<template>
  <a-spin :spinning="confirmLoading">
    <j-form-container :disabled="formDisabled">
      <!-- 主表单区域 -->
      <a-form-model ref="form" :model="model" :rules="validatorRules" slot="detail">
        <a-row>
          <a-col :span="12" >
            <a-form-model-item label="姓名" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="name">
              <a-input v-model="model.name" placeholder="请输入姓名" ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="12" >
            <a-form-model-item label="性别" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="sex">
              <j-dict-select-tag type="list" v-model="model.sex" dictCode="sex" placeholder="请选择性别" />
            </a-form-model-item>
          </a-col>
          <a-col :span="12" >
            <a-form-model-item label="证件类型" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="idType">
              <j-dict-select-tag type="list" v-model="model.idType" dictCode="id_type" placeholder="请选择证件类型" />
            </a-form-model-item>
          </a-col>
          <a-col :span="12" >
            <a-form-model-item label="身份证号" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="idNo">
              <a-input v-model="model.idNo" placeholder="请输入身份证号" ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="12" >
            <a-form-model-item label="民族" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="nation">
              <j-dict-select-tag type="list" v-model="model.nation" dictCode="hl_nation" placeholder="请选择民族" />
            </a-form-model-item>
          </a-col>
          <a-col :span="12" >
            <a-form-model-item label="政治面貌" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="faceType">
              <a-input v-model="model.faceType" placeholder="请输入政治面貌" ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="12" >
            <a-form-model-item label="生日" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="birthday">
              <j-date placeholder="请选择生日" v-model="model.birthday" style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :span="12" >
            <a-form-model-item label="籍贯" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="nativePlace">
              <a-input v-model="model.nativePlace" placeholder="请输入籍贯" ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="12" >
            <a-form-model-item label="老人类型" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="oldType">
              <j-dict-select-tag type="list" v-model="model.oldType" dictCode="hl_old_type" placeholder="请选择老人类型" />
            </a-form-model-item>
          </a-col>
          <a-col :span="12" >
            <a-form-model-item label="自理能力" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="self">
              <j-dict-select-tag type="list" v-model="model.self" dictCode="hl_old_self" placeholder="请选择自理能力" />
            </a-form-model-item>
          </a-col>
          <a-col :span="12" >
            <a-form-model-item label="户籍地" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="domicile">
		      <j-area-linkage type="cascader" v-model="model.domicile" placeholder="请输入省市区" />
            </a-form-model-item>
          </a-col>
          <a-col :span="12" >
            <a-form-model-item label="现居住地" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="liveAdress">
		      <j-area-linkage type="cascader" v-model="model.liveAdress" placeholder="请输入省市区" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="备注" :labelCol="labelCol2" :wrapperCol="wrapperCol2" prop="remark">
              <a-textarea v-model="model.remark" rows="4" placeholder="请输入备注" />
            </a-form-model-item>
          </a-col>
        </a-row>
      </a-form-model>
    </j-form-container>
      <!-- 子表单区域 -->
    <a-tabs v-model="activeKey" @change="handleChangeTabs">
      <a-tab-pane tab="联系人信息" :key="refKeys[0]" :forceRender="true">
        <j-editable-table
          :ref="refKeys[0]"
          :loading="hlContactsInfoTable.loading"
          :columns="hlContactsInfoTable.columns"
          :dataSource="hlContactsInfoTable.dataSource"
          :maxHeight="300"
          :disabled="formDisabled"
          :rowNumber="true"
          :rowSelection="true"
          :actionButton="true"/>
      </a-tab-pane>
      <a-tab-pane tab="健康信息" :key="refKeys[1]" :forceRender="true">
        <hl-user-health-form ref="hlUserHealthForm" @validateError="validateError" :disabled="formDisabled"></hl-user-health-form>
      </a-tab-pane>

      <a-tab-pane tab="老人床位信息" :key="refKeys[2]" :forceRender="true">
        <hl-user-bed-form ref="hlUserBedForm" @validateError="validateError" :disabled="formDisabled"></hl-user-bed-form>
      </a-tab-pane>

    </a-tabs>
  </a-spin>
</template>

<script>

  import { getAction } from '@/api/manage'
  import { FormTypes,getRefPromise,VALIDATE_NO_PASSED } from '@/utils/JEditableTableUtil'
  import { JEditableTableModelMixin } from '@/mixins/JEditableTableModelMixin'
  import { validateDuplicateValue } from '@/utils/util'
  import HlUserHealthForm from './HlUserHealthForm.vue'
  import HlUserBedForm from './HlUserBedForm.vue'

  export default {
    name: 'HlUserForm',
    mixins: [JEditableTableModelMixin],
    components: {
      HlUserHealthForm,
      HlUserBedForm,
    },
    data() {
      return {
        labelCol: {
          xs: { span: 24 },
          sm: { span: 6 },
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 },
        },
        labelCol2: {
          xs: { span: 24 },
          sm: { span: 3 },
        },
        wrapperCol2: {
          xs: { span: 24 },
          sm: { span: 20 },
        },
        model:{
        },
        // 新增时子表默认添加几行空数据
        addDefaultRowNum: 1,
        validatorRules: {
          name: [
            { required: true, message: '请输入姓名!'},
          ],
          sex: [
            { required: true, message: '请输入性别!'},
          ],
          nation: [
            { required: true, message: '请输入民族!'},
          ],
          birthday: [
            { required: true, message: '请输入生日!'},
          ],
          domicile: [
            { required: true, message: '请输入户籍地!'},
          ],
          liveAdress: [
            { required: true, message: '请输入现居住地!'},
          ],
        },
        refKeys: ['hlContactsInfo', 'hlUserHealth',  'hlUserBed',],
        tableKeys:['hlContactsInfo', ],
        activeKey: 'hlContactsInfo',
        // 联系人信息
        hlContactsInfoTable: {
          loading: false,
          dataSource: [],
          columns: [
            {
              title: '姓名',
              key: 'name',
              type: FormTypes.input,
              width:"200px",
              placeholder: '请输入${title}',
              defaultValue:'',
              validateRules: [{ required: true, message: '${title}不能为空' }],
            },
            {
              title: '联系电话',
              key: 'phone',
              type: FormTypes.input,
              width:"200px",
              placeholder: '请输入${title}',
              defaultValue:'',
              validateRules: [{ pattern: "m", message: "${title}格式不正确" }],
            },
            {
              title: '关系',
              key: 'type',
              type: FormTypes.select,
              dictCode:"contact_relation",
              width:"200px",
              placeholder: '请输入${title}',
              defaultValue:'',
            },
            {
              title: '地址',
              key: 'address',
              type: FormTypes.select,
              dictCode:"",
              width:"200px",
              placeholder: '请输入${title}',
              defaultValue:'',
            },
            {
              title: '备注',
              key: 'remark',
              type: FormTypes.input,
              width:"200px",
              placeholder: '请输入${title}',
              defaultValue:'',
            },
          ]
        },
        // 健康信息
        hlUserHealthTable: {
          loading: false,
          dataSource: [],
          columns: [
          ]
        },
        // 老人床位信息
        hlUserBedTable: {
          loading: false,
          dataSource: [],
          columns: [
          ]
        },
        url: {
          add: "/health/hlUser/add",
          edit: "/health/hlUser/edit",
          queryById: "/health/hlUser/queryById",
          hlContactsInfo: {
            list: '/health/hlUser/queryHlContactsInfoByMainId'
          },
          hlUserHealth: {
            list: '/health/hlUser/queryHlUserHealthByMainId'
          },

          hlUserBed: {
            list: '/health/hlUser/queryHlUserBedByMainId'
          },
        }
      }
    },
    props: {
      //表单禁用
      disabled: {
        type: Boolean,
        default: false,
        required: false
      }
    },
    computed: {
      formDisabled(){
        return this.disabled
      },
    },
    created () {
    },
    methods: {
      addBefore(){
        this.hlContactsInfoTable.dataSource=[]
        this.$refs.hlUserHealthForm.clearFormData()
        this.$refs.hlUserBedForm.clearFormData()
      },
      getAllTable() {
        let values = this.tableKeys.map(key => getRefPromise(this, key))
        return Promise.all(values)
      },
      /** 调用完edit()方法之后会自动调用此方法 */
      editAfter() {
        this.$nextTick(() => {
          this.$refs.hlUserHealthForm.initFormData(this.url.hlUserHealth.list,this.model.id)
          this.$refs.hlUserBedForm.initFormData(this.url.hlUserBed.list,this.model.id)
        })
        // 加载子表数据
        if (this.model.id) {
          let params = { id: this.model.id }
          this.requestSubTableData(this.url.hlContactsInfo.list, params, this.hlContactsInfoTable)
        }
      },
      //校验所有一对一子表表单
      validateSubForm(allValues){
          return new Promise((resolve,reject)=>{
            Promise.all([
                this.$refs.hlUserHealthForm.validate(1),
                this.$refs.hlUserBedForm.validate(2),
            ]).then(() => {
              resolve(allValues)
            }).catch(e => {
              if (e.error === VALIDATE_NO_PASSED) {
                // 如果有未通过表单验证的子表，就自动跳转到它所在的tab
                this.activeKey = e.index == null ? this.activeKey : this.refKeys[e.index]
              } else {
                console.error(e)
              }
            })
          })
      },
      /** 整理成formData */
      classifyIntoFormData(allValues) {
        let main = Object.assign(this.model, allValues.formValue)
        return {
          ...main, // 展开
          hlContactsInfoList: allValues.tablesValue[0].values,
          hlUserHealthList: this.$refs.hlUserHealthForm.getFormData(),
          hlUserBedList: this.$refs.hlUserBedForm.getFormData(),
        }
      },
      validateError(msg){
        this.$message.error(msg)
      },

    }
  }
</script>

<style scoped>
</style>